//
//  MyVideoRecord.m
//  cooba-iOS
//
//  Created by 郭四虎 on 16/4/1.
//  Copyright © 2016年 YouKu. All rights reserved.
//

#import "MyVideoRecord.h"

@implementation MyVideoRecord

+ (instancetype)shareRecord {
    static id instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[[self class] alloc] init];
    });
    
    return instance;
}

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        if ([[EGOCache globalCache]objectForKey:[NSString stringWithFormat:@"%@videorecord",USERID]]) {
            
            _dataArr = (NSMutableArray *)[[EGOCache globalCache]objectForKey:[NSString stringWithFormat:@"%@videorecord",USERID]];
        }else{
            
            _dataArr = [NSMutableArray array];
        }
    }
    return _dataArr;
}

+ (void)addOneRecord:(VideoListModel *)model
{
    
    VideoListModel *newModel = [[self shareRecord]getNewModel:model];
    
    newModel.clickDate = [MyDateformater getCurrentDate];
    [[self shareRecord]addModel:newModel];
}

- (void)addModel:(VideoListModel *)model
{
    [self.dataArr insertObject:model atIndex:0];
}

- (VideoListModel *)getNewModel:(VideoListModel *)old
{
    VideoListModel *newModel = [[VideoListModel alloc]init];
    newModel.videoid = old.videoid;
    newModel.title = old.title;
    newModel.content= old.content;
    newModel.img_url= old.img_url;
    newModel.desc= old.desc;
    newModel.video_page_url= old.video_page_url;
    newModel.intro= old.intro;
    newModel.video_url= old.video_url;
    
    return newModel;
}

- (void)saveVideoRecord
{
    if (!USERID) {
        return;
    }
    NSString *key = [NSString stringWithFormat:@"%@videorecord",USERID];
    
    if (_dataArr.count) {
        
        [[EGOCache globalCache]setObject:_dataArr forKey:key];
    }else{
        [[EGOCache globalCache]setObject:nil forKey:key];
    }
}

- (void)delOneRecord:(VideoListModel *)model
{
    [_dataArr removeObject:model];
}

- (void)cleanVideoRecord
{
    if (_dataArr.count) {
        [_dataArr removeAllObjects];
    }
}

-     (NSDictionary*)getObjectData:(id)obj
     
    {
        
        NSMutableDictionary
        *dic = [NSMutableDictionary dictionary];
        
        unsigned
        int propsCount;
        
        objc_property_t
        *props = class_copyPropertyList([obj class], &propsCount);
        
        for(int
            i = 0;i < propsCount; i++)
            
        {
            
            objc_property_t
            prop = props[i];
            
            
            
            NSString
            *propName = [NSString stringWithUTF8String:property_getName(prop)];
            
            id
            value = [obj valueForKey:propName];
            
            if(value
               == nil)
                
            {
                
                value
                = [NSNull null];
                
            }
            
            else
                
            {
                
                value
                =  [self getObjectInternal:value];
                
            }
            
            [dic
             setObject:value forKey:propName];
            
        }
        
        return
        
        dic;
        
    }

-(id)getObjectInternal:(id)obj

{
    
    if([obj
        isKindOfClass:[NSString class]]
       
       ||
       [obj isKindOfClass:[NSNumber class]]
       
       ||
       [obj isKindOfClass:[NSNull class]])
        
    {
        
        return
        
        obj;
        
    }
    if([obj
        isKindOfClass:[NSArray class]])
        
    {
        
        NSArray
        *objarr = obj;
        
        NSMutableArray
        *arr = [NSMutableArray arrayWithCapacity:objarr.count];
        
        for(int
            i = 0;i < objarr.count; i++)
            
        {
            
            [arr
             setObject:[self getObjectInternal:[objarr objectAtIndex:i]] atIndexedSubscript:i];
            
        }
        
        return
        
        arr;
        
    }
    
    
    
    if([obj
        isKindOfClass:[NSDictionary class]])
        
    {
        
        NSDictionary
        *objdic = obj;
        
        NSMutableDictionary
        *dic = [NSMutableDictionary dictionaryWithCapacity:[objdic count]];
        
        for(NSString
            *key in
            
            objdic.allKeys)
            
        {
            
            [dic
             setObject:[self getObjectInternal:[objdic objectForKey:key]] forKey:key];
            
        }     
        
        return
        
        dic;
        
    } 
    
    return
    
    [self getObjectData:obj];
    
}
//     - (NSDictionary *)dictionaryRepresentation {
//         unsigned int count = 0;
//         //get a list of all properties of this class
//         objc_property_t *properties = class_copyPropertyList([self class], &count);
//         NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:count];
//         
//         NSDictionary *keyValueMap = [self attributeMapDictionary];
//         
//         for (int i = 0; i < count; i++) {
//             NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
//             id value = [self valueForKey:key];
//             NSLog(@"key = %@, value = %@, value class = %@, changed Key = %@", key, value, NSStringFromClass([value class]), [keyValueMap objectForKey:key]);
//             key = [keyValueMap objectForKey:key];
//             //only add it to dictionary if it is not nil
//             if (key && value) {
//                 [dict setObject:value forKey:key];
//             }
//         }
//         
//         free(properties);
//         return dict;
//     }

@end
